package com.logback.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;

/**
 * @Author zqf
 * @Date 2023/3/21 14:47
 * @Description: 反射(获取class对象)
 */
public class ReflectionTest2 {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionTest2.class);

    public static void main(String[] args) {

        //体会反射的动态性
        for (int i = 0; i < 3; i++) {
            //0,1,2
            String classPath = "";
            switch (i) {
                case 0:
                    classPath = "java.util.Date";
                    break;
                case 1:
                    classPath = "java.lang.Object";
                    break;
                case 2:
                    classPath = "com.logback.demo.People";
                    break;

                default:
                    break;
            }

            try {
                Object obj = getInstance(classPath);
                System.out.println(obj);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
    创建一个指定类的对象。
    classPath:指定类的全类名
     */
    public static Object getInstance(String classPath) throws Exception {
        Class clazz = Class.forName(classPath);
        return clazz.newInstance();
    }

}
